<template>
    <a-form :form="form" class="card-area">
      <a-spin :spinning="spinning">
       <div v-for="(xm,index) in xmlist" :key="xm.id">
        <a-card class="card-area"  :title="xm.projectName" :style="index?'margin-top:20px;':''">
        <a-row :gutter="48">
        <a-col :sm="{span:24}" :lg="{span:12}">
          <a-form-item
            label="项目申请时间"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
          <a-range-picker
              showTime
              :default-value="xm.applyStartTime&&xm.applyEndTime?[moment(xm.applyStartTime, 'YYYY-MM-DD'), moment(xm.applyEndTime, 'YYYY-MM-DD')]:''"
              format="YYYY-MM-DD"
              v-model="xm.applyTime"
            />
          </a-form-item>
        </a-col>
        <a-col :sm="{span:24}" :lg="{span:12}">
          <a-form-item
            label="回校申请时间"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-range-picker
              showTime
              :default-value="xm.returnStartTime&&xm.returnEndTime?[moment(xm.returnStartTime, 'YYYY-MM-DD'), moment(xm.returnEndTime, 'YYYY-MM-DD')]:''"
              format="YYYY-MM-DD"
              v-model="xm.returnTime"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row style="text-align: center" > <a-button type="primary" @click="handleSave(index)">保存</a-button></a-row>
       </a-card>
     </div>
     </a-spin>
    </a-form>
</template>
<script>
  import {mapState} from 'vuex'
  import moment from 'moment'
  export default {
  name: 'StuEditTime',
  data () {
    return {
      xmlist:[],
      applyTime: null,
      returnTime: null,
      endOpen: false,
      spinning:false
    }

  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo
    })
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {
    this.initSelectMenu();
  },
  methods: {
    moment,
    initSelectMenu () {
      let allRequest = [
        this.$api.student.reportBasic(),
      ]
      Promise.all(allRequest).then(res => {
          console.log("res",res)
          this.xmlist=res[0].data.xmsb

      })
    },
    // 保存操作
    handleSave (index) {
      var params = {}
      params.id = this.xmlist[index].id
      this.spinning = true
      if(this.xmlist[index].applyStartTime && this.xmlist[index].applyEndTime){
        params.applyStartTime = this.xmlist[index].applyStartTime
        params.applyEndTime = this.xmlist[index].applyEndTime
      }
      if(this.xmlist[index].returnStartTime && this.xmlist[index].returnEndTime){
        params.returnStartTime = this.xmlist[index].returnStartTime
        params.returnEndTime = this.xmlist[index].returnEndTime
      }
      if(this.xmlist[index].applyTime && this.xmlist[index].applyTime.length){
       params.applyStartTime = this.xmlist[index].applyTime[0].format('YYYY-MM-DD')
       params.applyEndTime = this.xmlist[index].applyTime[1].format('YYYY-MM-DD')
      }
      //  if(this.xmlist[index].applyTime && this.xmlist[index].applyTime.length == 0){
      //  params.applyStartTime = ''
      //  params.applyEndTime = ''
      // }
      if(this.xmlist[index].returnTime && this.xmlist[index].returnTime.length){
       params.returnStartTime = this.xmlist[index].returnTime[0].format('YYYY-MM-DD')
       params.returnEndTime = this.xmlist[index].returnTime[1].format('YYYY-MM-DD')
      }
      // if(this.xmlist[index].returnTime && this.xmlist[index].returnTime.length == 0){
      //  params.returnStartTime = ''
      //  params.returnEndTime = ''
      // }
      this.$api.student.timeSet(params).then(res => {
        if(res.status == 1){
          this.$message.success(res.msg)
        }else{
          this.$message.warning(res.msg)
        }
      }).finally(res =>{
        this.spinning = false
      })
    }
  }
}
</script>
